package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.AddressBiz;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.xiaoleilu.hutool.util.CollectionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by THINK on 2017/4/27.
 */
@Controller
@RequestMapping("/admin/address")
public class AddressAdminManageAction {

    @Autowired
    private AddressBiz addressBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        List<Address> addressList = addressBiz.queryObjects("select o from Address o where o.deleteStatus = 0 and o.level = 3", null);
        mv.put("addressList", addressList);
        return "/admin/address/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "sidx", required = false) String sidxParam,
                               @RequestParam(value = "sord", required = false) String sordParam,
                               @RequestParam(value = "addrName", required = false) String addrNameParam,
                               @RequestParam(value = "level", required = false) String levelParam,
                               @RequestParam(value = "areaId", required = false) String areaIdParam,
                               @RequestParam(value = "parentName", required = false) String parentNameParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        if(!CommUtil.null2String(sidxParam).equals("") && !CommUtil.null2String(sordParam).equals("") ) {
            queryParam.setSqlOrder("order by s." + sidxParam + " " + sordParam);
        }

        if (!CommUtil.null2String(addrNameParam).equals("")) {
            queryParam.getSqlMap().put("addrName",addrNameParam);
        }if (!CommUtil.null2String(areaIdParam).equals("")) {
            queryParam.getSqlMap().put("parent_id",areaIdParam);
        }
        if (!CommUtil.null2String(levelParam).equals("")){
            queryParam.getSqlMap().put("level",levelParam);
        }
        queryParam.getSqlMap().put("levelMax", "2");
        if (!CommUtil.null2String(parentNameParam).equals("")) {
            queryParam.getSqlMap().put("parentName",parentNameParam);
        }

        return addressBiz.findJSONList(queryParam);
    }

    @RequestMapping("/add")
    @ResponseBody
    public JSONObject add(HttpServletRequest request) {

        String addrName = request.getParameter("addrName");
        String parentAddr = request.getParameter("parentAddr");
        String addrCode = request.getParameter("addrCode");
        return addressBiz.addObs(addrName, parentAddr, addrCode);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public JSONObject edit(HttpServletRequest request) {

        String id = request.getParameter("id");
        String addrName = request.getParameter("addrName");
        String parentAddr = request.getParameter("parentAddr");
        String addrCode = request.getParameter("addrCode");
        return addressBiz.editObs(id, addrName, parentAddr, addrCode);
    }

    @RequestMapping(value = "/editPage", produces = "text/html;charset=UTF-8")
    public String edit(HttpServletRequest request, ModelMap mv) {

        List<Address> addressList = addressBiz.queryObjects("select o from Address o where o.deleteStatus = 0 and o.level = 3", null);
        mv.put("addressList", addressList);

        String id = request.getParameter("id");
        Address address = addressBiz.findModel(CommUtil.null2Long(id));
        mv.put("addr", address);
        return "/admin/address/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request, ModelMap mv) {

        String id = request.getParameter("id");
        return addressBiz.delete(id);
    }

    @RequestMapping("/findByParentID")
    @ResponseBody
    public JSONObject findById(HttpServletRequest request,String id) {
        JSONObject jsonObject;
        JSONArray jsonArray = new JSONArray();
        if(CommUtil.null2Long(id)>0) {
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("parent_id", id);
            List<Address>addressList = addressBiz.findList(queryParam);
            for(Address address:addressList){
                jsonObject = new JSONObject();
                jsonObject.put("id",address.getId());
                jsonObject.put("name",address.getAddrName());
                jsonArray.add(jsonObject);
            }
        }
        jsonObject = new JSONObject();
        jsonObject.put("result",jsonArray);
        return jsonObject;
    }

    @RequestMapping("/getAddrListByPid")
    public String getAddrListByPid(ModelMap mv ,String pid) {

        mv.put("addrList", addressBiz.getAddrList(CommUtil.null2Long(pid)));
        return "/admin/address/part";
    }

    @RequestMapping("/areaList")
    @ResponseBody
    public JSONObject areaList() {

        JSONObject ret = CommUtil.jsonResult(true);
        List<Address> addressList = addressBiz.getAddrList(null);
        JSONArray array = new JSONArray();
        JSONObject object = null;
        if(CollectionUtil.isNotEmpty(addressList)) {
            for(Address address : addressList) {
                object = new JSONObject();
                object.put("id", address.getId());
                object.put("addrName", address.getAddrName());
                array.add(object);
            }
        }
        ret.put("contents", array);
        return ret;
    }
}
